﻿function AzureOnlineTablesDataContext(context) {
    var self = this;

    var table = ko.observable('');

    var tables = ko.observableArray([]);

    var records = ko.observableArray([]);

    var markers = ko.observableArray([]);

    var schema = ko.computed(function () {
        var recs = records();
        var res = new AzureOnlineTableSchema(table());
        res.forRecords(recs);
        return res;
    });

    var tablesSettings = ko.computed(function() {
        var as = context.accountSettings();
        return as.folder('tables');
    });
    
    var tableSettings = ko.computed(function () {
        return tablesSettings().folder(table());
    });

    function setRawMarkers(rawMarkers) {
        var res = [];
        for (var i = 0; i < rawMarkers.length; i++) {
            var marker = new AzureOnlineTableMarker();
            marker.setInfo(rawMarkers[i]);
            res.push(marker);
        }
        while (res.length < 5) {
            res.push(new AzureOnlineTableMarker());
        }
        markers(res);
    }
    
    function resetMarkers() {
        var rawMarkers = [];
        var tableName = table();
        switch (tableName) {
            case 'WADLogsTable':
                rawMarkers.push({
                    color: 'red',
                    filter: 'Level ge 1 and Level le 2'
                });
                rawMarkers.push({
                    color: 'yellow',
                    filter: 'Level eq 3'
                });
                break;
        }
        setRawMarkers(rawMarkers);
    }
    
    ko.computed(function () {
        var settings = tableSettings();
        
        var rawMarkers = settings.getObject('markers');
        if (rawMarkers) {
            setRawMarkers(rawMarkers);
        } else {
            resetMarkers();
        }
    });
    
    var tableColumns = new AzureOnlineTableColumnsView();

    ko.computed(function () {
        var flds = schema().fields();
        tableColumns.init(flds, tableSettings());
    });

    function addNewTable(tbl) {
        var list = tables();
        list.push(tbl);
        list.sort(function (a, b) {
            var an = a.name();
            var bn = b.name();
            if (an > bn) return 1;
            if (an < bn) return -1;
            return 0;
        });
        tables(list);
    }

    function findByName(name) {
        var items = tables();
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            if (item.name() == name) return item;
        }
        return null;
    }

    function removeTable() {
        tables.remove(table);
    }

    self.table = table;
    self.tables = tables;
    self.records = records;
    self.schema = schema;
    self.markers = markers;
    self.resetMarkers = resetMarkers;
    self.tablesSettings = tablesSettings;
    self.tableSettings = tableSettings;
    self.tableColumns = tableColumns;
    self.addNewTable = addNewTable;
    self.findByName = findByName;
    self.removeTable = removeTable;
}